package com.xbongbong.crm.handle.analysis;

import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.VisibleRulePoJo;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.parse.data.AbstractListAnalysisDataService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.help.SaasHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户解析合同的方法
 * @author 吴峰
 * @date 2022/05/16 9:33
 *
 */
@Service("crmListAnalysisDataHandler")
@Slf4j
public class ContractListAnalysisDataHandler extends AbstractListAnalysisDataService<PaasFormDataEntityExt> {

    @Resource
    private SaasHelp saasHelp;

    @Override
    public List<Integer> businessList() {
        return Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode());
    }


    /**
     * 解析数据
     * @param formDataListVO
     * @throws XbbException
     */
    @Override
    public void analysisDataList(FormDataListDTO formDataListDTO, PaasFormDataEsListVO formDataListVO) throws XbbException {
        saasHelp.handleListSpecialField(formDataListVO, XbbRefTypeEnum.CONTRACT.getCode());
        super.analysisDataList(formDataListDTO, formDataListVO);
    }

    /**
     * 将合同订单中应收、未收与合同金额可见权限一致
     * @param explainList
     * @return
     */
    public static List<FieldAttrEntity> setAmountVisible( List<FieldAttrEntity> explainList) {
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(explainList.size());
        explainList.forEach(item->{
            explainMap.put(item.getAttr(), item);
        });
        if (explainMap.containsKey(ContractEnum.AMOUNT.getAttr())){
            VisibleRulePoJo visibleRulePoJo = explainMap.get(ContractEnum.AMOUNT.getAttr()).getVisibleScopeRule();
            Integer visibleScopeEnable = explainMap.get(ContractEnum.AMOUNT.getAttr()).getVisibleScopeEnable();
            Integer visible = explainMap.get(ContractEnum.AMOUNT.getAttr()).getVisible();
            if (Objects.equals(visibleScopeEnable,BasicConstant.ONE)){
                //处理实付金额
                FieldAttrEntity fieldAttrEntity = explainMap.get(ContractEnum.FINISH_AMOUNT.getAttr());
                if (Objects.nonNull(fieldAttrEntity)) {
                    fieldAttrEntity.setVisible(visible);
                    fieldAttrEntity.setVisibleScopeEnable(visibleScopeEnable);
                    fieldAttrEntity.setVisibleScopeRule(visibleRulePoJo);
                    explainMap.put(ContractEnum.FINISH_AMOUNT.getAttr(), fieldAttrEntity);
                }
                //处理未付金额
                fieldAttrEntity = explainMap.get(ContractEnum.UN_FINISH_AMOUNT.getAttr());
                if (Objects.nonNull(fieldAttrEntity)) {
                    fieldAttrEntity.setVisible(visible);
                    fieldAttrEntity.setVisibleScopeEnable(visibleScopeEnable);
                    fieldAttrEntity.setVisibleScopeRule(visibleRulePoJo);
                    explainMap.put(ContractEnum.UN_FINISH_AMOUNT.getAttr(), fieldAttrEntity);
                }
                //处理坏账金额
                fieldAttrEntity = explainMap.get(ContractEnum.BAD_AMOUNT.getAttr());
                if (Objects.nonNull(fieldAttrEntity)) {
                    fieldAttrEntity.setVisible(visible);
                    fieldAttrEntity.setVisibleScopeEnable(visibleScopeEnable);
                    fieldAttrEntity.setVisibleScopeRule(visibleRulePoJo);
                    explainMap.put(ContractEnum.BAD_AMOUNT.getAttr(), fieldAttrEntity);
                }
                //处理收款比例
                fieldAttrEntity = explainMap.get(ContractEnum.PAY_PERCENT.getAttr());
                if (Objects.nonNull(fieldAttrEntity)) {
                    fieldAttrEntity.setVisible(visible);
                    fieldAttrEntity.setVisibleScopeEnable(visibleScopeEnable);
                    fieldAttrEntity.setVisibleScopeRule(visibleRulePoJo);
                    explainMap.put(ContractEnum.PAY_PERCENT.getAttr(), fieldAttrEntity);
                }
                //处理开票金额
                fieldAttrEntity = explainMap.get(ContractEnum.INVOICE_AMOUNT.getAttr());
                if (Objects.nonNull(fieldAttrEntity)) {
                    fieldAttrEntity.setVisible(visible);
                    fieldAttrEntity.setVisibleScopeEnable(visibleScopeEnable);
                    fieldAttrEntity.setVisibleScopeRule(visibleRulePoJo);
                    explainMap.put(ContractEnum.INVOICE_AMOUNT.getAttr(), fieldAttrEntity);
                }
                //处理未开票金额
                fieldAttrEntity = explainMap.get(ContractEnum.UN_INVOICE_AMOUNT.getAttr());
                if (Objects.nonNull(fieldAttrEntity)) {
                    fieldAttrEntity.setVisible(visible);
                    fieldAttrEntity.setVisibleScopeEnable(visibleScopeEnable);
                    fieldAttrEntity.setVisibleScopeRule(visibleRulePoJo);
                    explainMap.put(ContractEnum.UN_INVOICE_AMOUNT.getAttr(), fieldAttrEntity);
                }
            }
        }
        return new ArrayList<>(explainMap.values());
    }
}
